//
//  FirstColumnViewController.m
//  Project_B
//
//  Created by 张闯的mac pro on 15/11/28.
//  Copyright © 2015年 张闯. All rights reserved.
//

#import "FirstColumnViewController.h"
#import "AFNetworkHandler.h"
#import "FirstModel.h"
#import "UIImageView+WebCache.h"
#import "ColumnTableViewCell.h"
#import "FirstTopViewViewController.h"
#import "MJRefresh.h"
#import "MBProgressHUD.h"
#import "UIImage+GIF.h"

@interface FirstColumnViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *ColumnTableView;
@property (nonatomic, strong) NSMutableArray *ColumnDataArr;
@property (nonatomic, strong) NSMutableDictionary *headViewDic;
@property (nonatomic, assign) NSInteger i;
@property (nonatomic, strong) MBProgressHUD *hud;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UIImageView *imgView;

@end

@implementation FirstColumnViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
//    self.navigationController.navigationBarHidden = YES;
        self.ColumnDataArr = [NSMutableArray array];
        self.headViewDic = [NSMutableDictionary dictionary];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui@2x"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    [self getColumnData];
    [self createColumnTableView];
    
    self.i = 1;
    
    __weak typeof(self) weakSelf = self;
    self.ColumnTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        __strong typeof(self) strongSelf = weakSelf;
        if (strongSelf) {
            [strongSelf footRefresh];
        } else {
            return;
        }
    }];
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:NO];
    self.hud.animationType = MBProgressHUDAnimationFade;
    
}

- (void)footRefresh
{
    NSString *str = [NSString stringWithFormat:@"http://api.lis99.com/v3/club/tagTopic/%ld/", self.i++];
    NSString *bodyStr = [NSString stringWithFormat:@"tagid=%@", self.idString];
    NSData *dataBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    [AFNetworkHandler requestPOSTWithURL:str body:dataBody comletion:^(id result) {
        
        self.headViewDic = result[@"data"][@"taginfo"];
        self.navigationItem.title = self.headViewDic[@"name"];
        [self getHeadViewData];
        
        NSArray *arr = [FirstModel transformWithArray:result[@"data"][@"topiclist"]];
        [self.ColumnDataArr addObjectsFromArray:arr];
        [self.ColumnTableView reloadData];
        [self.ColumnTableView.mj_footer endRefreshing];
    }];

}



- (void)getColumnData
{
    NSString *str = [NSString stringWithFormat:@"http://api.lis99.com/v3/club/tagTopic/0/"];
    NSString *bodyStr = [NSString stringWithFormat:@"tagid=%@", self.idString];
    NSData *dataBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    [AFNetworkHandler requestPOSTWithURL:str body:dataBody comletion:^(id result) {
        
        self.headViewDic = result[@"data"][@"taginfo"];
        self.navigationItem.title = self.headViewDic[@"name"];
        [self getHeadViewData];
        
        self.ColumnDataArr = [FirstModel transformWithArray:result[@"data"][@"topiclist"]];
        [self.ColumnTableView reloadData];
        [self.view addSubview:self.ColumnTableView];
        [self.hud removeFromSuperview];
    }];

}

-(void)getHeadViewData
{
    self.label.text = self.headViewDic[@"name"];
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:self.headViewDic[@"tag_images"]] placeholderImage:[UIImage imageNamed:@""]];
}

- (void)createColumnTableView
{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 150)];
    self.imgView = [[UIImageView alloc] initWithFrame:headView.frame];
    self.imgView.backgroundColor = [UIColor orangeColor];
    [headView addSubview:self.imgView];
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
    self.label.center = headView.center;
    //[headView addSubview:label];
    self.label.backgroundColor = [UIColor blackColor];
    self.label.textColor = [UIColor whiteColor];
    self.label.textAlignment = NSTextAlignmentCenter;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    //[headView addSubview:button];
    button.frame = CGRectMake(10, 20, 30, 30);
    button.backgroundColor = [UIColor orangeColor];
    [button addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.ColumnTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667 - 64) style:UITableViewStylePlain];
    self.ColumnTableView.dataSource = self;
    self.ColumnTableView.delegate = self;
    self.ColumnTableView.tableHeaderView = headView;
    
    [self.ColumnTableView registerClass:[ColumnTableViewCell class] forCellReuseIdentifier:@"cell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.ColumnDataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ColumnTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = self.ColumnDataArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 160;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    FirstTopViewViewController *topVC = [[FirstTopViewViewController alloc] init];
    topVC.idString = [self.ColumnDataArr[indexPath.row] topic_id];
    [self.navigationController pushViewController:topVC animated:YES];
}


#pragma mark - backAction
- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
